import { ApiRes, Category, SubCategory, TopCategory } from "@/types/data";
import request from "@/utils/request";
import { defineStore } from "pinia";
import {topCategory} from '@/store/constants'
const defaultCategory = topCategory.map(item=>{
  return {name:item}
})
const useCategoryStore = defineStore('category',{
  state:()=>{
   return {
    list:defaultCategory as Category[],
    topCategory:{} as TopCategory,
    subCategory:{} as SubCategory
   }
  },
  actions:{
    async  getAllCategory(){
      const res = await request.get<ApiRes<Category[]>>('/home/category/head')
      res.data.result.forEach(item=>item.open=false)
      this.list = res.data.result
    },
    // 控制二级弹框显示隐藏
    show(id:string ){
    const category =  this.list.find(item=>item.id===id)
    category!.open = true
    },
    hide(id:string ){
      const category =  this.list.find(item=>item.id===id)
      category!.open = false
      },
    async getTopCategory(id:string){
      const res = await request.get<ApiRes<TopCategory>>('/category',{
        params:{
          id,
        }
      })
      this.topCategory = res.data.result
    },
    // 二级分类
    async getSubFilter(id: string) {
      const res = await request.get<ApiRes<SubCategory>>(
        '/category/sub/filter',
        {
          params: {
            id,
          },
        }
      )
      this.subCategory = res.data.result
    },
  }
})
export default useCategoryStore
